package com.unicorn.modual.user.controller;

import com.unicorn.base.BaseController;
import com.unicorn.base.Routes;
import com.unicorn.entity.Users;
import com.unicorn.modual.user.service.UserService;
import com.unicorn.util.JsonResponse;
import com.unicorn.util.common.APIVersion;
import com.unicorn.vo.UsersVO;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by windy on 16-11-3.
 * 系统后台用户管理
 */

@RestController
public class UserController extends BaseController {

    private static Logger logger= LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
     *
     * @param request
     * @param response
     * @return /system/user/list
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = Routes.SYSTEM_USER_LIST,method = RequestMethod.GET)
    public JsonResponse listUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hello world..");
        List<Users> result = userService.listUsers();
        return requestHandler.handleRequest(result,request,response,logger);

    }

    /**
     *
     * @param usersVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */

    @RequestMapping(value = Routes.SYSTEM_USER_SAVE,method = RequestMethod.POST)
    public  JsonResponse saveUserInfo(@RequestBody  UsersVO usersVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hello world..");
        Users user = userService.saveUser(usersVO);
        return requestHandler.handleRequest(user,request,response,logger);
    }

    /**
     *
     * @param usersVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @APIVersion(value ="1.0.0")
    @RequestMapping(value = Routes.SYSTEM_USER_DELETE,method = RequestMethod.DELETE)
    public  JsonResponse delUserInfo(@RequestBody  UsersVO usersVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("hello world..");
        boolean isSuccess = userService.deleteUsersById(usersVO.getId());
        if(isSuccess){
            requestHandler.warning(UsersVO.MSG_DELETE_SUCCESS,logger);
        }else{
            requestHandler.warning(UsersVO.MSG_DELETE_FAIL,logger);
        }
        return requestHandler.handleRequest(request,response,logger);
    }

    /**
     *
     * @param usersVO
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = Routes.SYSTEM_USER_UPDATE,method = RequestMethod.POST)
    public JsonResponse updateUserInfo(@RequestBody  UsersVO usersVO, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if(usersVO.getId()==null){
            jsonResponse.setMessage(UsersVO.MSG_MISS_DATA_ID);
            jsonResponse.setCode(JsonResponse.STATUS_FAIL);
            return requestHandler.handleRequest(request,response,logger);
        }
        Users user = userService.updateUser(usersVO);
        return requestHandler.handleRequest(user,request,response,logger);
    }

}
